package atguigu.boot;

import atguigu.boot.bean.Pet;
import atguigu.boot.bean.User;
import atguigu.boot.config.MyConfig;
import ch.qos.logback.core.db.DBHelper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;

/**
 * @program: springboot-01-helloworld
 * @ClassName MainApplication
 * @description: 主程序类
 * @author: zwq
 **/
//@SpringBootApplication(scanBasePackages = "atguigu")      //可用于改变包扫描路径
@SpringBootApplication
//@ImportResource注解作用：引入原生配置文件
@ImportResource("classpath:beans.xml")
public class MainApplication {

    public static void main(String[] args) {
        //1.返回我们的IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        //2.查看容器里面的组件
        String[] names = run.getBeanDefinitionNames();
        for(String name:names){
            System.out.println(name);
        }
        //3.从容器中获取组件
        Pet cat2 = run.getBean("cat", Pet.class);
        Pet cat3 = run.getBean("cat", Pet.class);
        System.out.println("组件："+(cat2==cat3));

        //4.通过注解给容器中添加组件bean
//        boolean tomcat001 = run.containsBean("tomcat001");
//        System.out.println("容器中tomcatoo1组件："+tomcat001);
//        boolean tomcat002 = run.containsBean("tomcat002");
//        System.out.println("容器中tomcatoo2组件："+tomcat002);
//        boolean user01 = run.containsBean("user01");
//        System.out.println("容器中user01组件："+user01);
//
//        //5.通过xml配置文件注册的bean
//        boolean cat = run.containsBean("cat");
//        System.out.println("容器中cat组件："+cat);
//        boolean cat1 = run.containsBean("cat1");
//        System.out.println("容器中cat1组件："+cat1);
//
//        MyConfig bean = run.getBean(MyConfig.class);
//        System.out.println("[MyCongig]:"+bean);
//        User user1 = bean.user01();
//        User user2 = bean.user01();
//        System.out.println(user1==user2);
//
//        //6.获取组件
//        String[] beanNamesForType = run.getBeanNamesForType(User.class);
//        System.out.println("==========");
//        for (String s : beanNamesForType) {
//            System.out.println(s);
//        }
//        DBHelper bean1 = run.getBean(DBHelper.class);
//        System.out.println(bean1);

        //@containsBean组件：是否有某个组件
        boolean tomcat = run.containsBean("tomcat");
        System.out.println("容器中tomcat组件："+tomcat);

        boolean user01 = run.containsBean("user01");
        System.out.println("容器中user01组件："+user01);
    }
}
